package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.Changecoachthree;
import com.gljx.web.Entity.Changecoachthreeentry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.ChangecoachthreeMapper;
import com.gljx.web.dao.ChangecoachthreeentryMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.service.ChangecoachThreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/28 0028.
 */
@Transactional
@Service
public class ChangecoachThreeServiceImpl extends CommonDao implements ChangecoachThreeService {

    @Autowired
    ChangecoachthreeMapper changecoachthreeMapper;
    @Autowired
    ChangecoachthreeentryMapper changecoachthreeentryMapper;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.ChangecoachthreeMapper.pagemap", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 下午 3:12
     * 查询子类
     */
    @Override
    public List<Changecoachthreeentry> selectChangecoachThreeentryByFid(String fid, String cfstudentname) {
        return changecoachthreeentryMapper.selectChangecoachThreeentryByFparentid(fid, cfstudentname);
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 下午 3:12
     * 添加
     */
    @Override
    public void addChangecoachThree(Changecoachthree changecoachthree, String[] cfstudentid, String[] cfstudentno,
                                    String[] cfmobile, String[] cfphone, String[] cfcoachid2, String[] cfcoachid,
                                    String[] cfcoachno, String[] cfremark, String state) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Changecoachthree changecoachthreetem = changecoachthreeMapper.selectByPrimaryKey(changecoachthree.getFid());
            changecoachthree.setFcreatorid(changecoachthreetem.getFcreatorid());
            changecoachthree.setFcreatetime(changecoachthreetem.getFcreatetime());
            changecoachthreeMapper.deleteByPrimaryKey(changecoachthree.getFid());
            changecoachthreeentryMapper.deleteByFparentid(changecoachthree.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(changecoachthree.getFfivouchered() + "")) {
                changecoachthree.setFauditorid(changecoachthreetem.getFauditorid());
                changecoachthree.setCfbillstate(Long.valueOf("3"));
            }
            if ("4".equals(changecoachthree.getFfivouchered() + "")) {
                Axamine axamine = new Axamine();
                axamine.submissionProcess(String.valueOf(changecoachthree.getFnumber()));
            }
        }

        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        changecoachthree.setFid(uuid);

        for (int i = 0; i < cfstudentid.length; i++) {
            Changecoachthreeentry changecoachthreeentry = new Changecoachthreeentry();
            changecoachthreeentry.setFid(GetFid.UUIDIntoFid());
            changecoachthreeentry.setFparentid(uuid);
            changecoachthreeentry.setCfstudentid(cfstudentid[i]);
            changecoachthreeentry.setCfstudentno(cfstudentno[i]);
            changecoachthreeentry.setCfmobile(cfmobile[i]);
            changecoachthreeentry.setCfphone(cfphone[i]);
            changecoachthreeentry.setCfcurrentcoachid(cfcoachid2[i]);
            changecoachthreeentry.setCfcoachid(cfcoachid[i]);
            changecoachthreeentry.setCfcoachno(cfcoachno[i]);
            changecoachthreeentry.setCfremark(cfremark[i]);

            changecoachthreeentryMapper.insertSelective(changecoachthreeentry);
        }
        changecoachthreeMapper.insertSelective(changecoachthree);
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 下午 3:12
     * 更新列表的查询
     */
    @Override
    public Changecoachthree selectChangecoachThreeByFid(String fid) {

        Changecoachthree changecoachthree = changecoachthreeMapper.selectByPrimaryKey(fid);

        if (changecoachthree != null) {
            changecoachthree.setChangecoachthreeentryList(changecoachthreeentryMapper.selectChangecoachThreeentryByFparentid(fid, ""));
        }
        return changecoachthree;
    }

    //删除
    @Override
    public void deleteByChangecoachThreeKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();

        for (int i = 0; i < deleteId.length; i++) {
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            changecoachthreeMapper.deleteByPrimaryKey(deleteId[i]);
            changecoachthreeentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    @Override
    public ServerResponse examine(Changecoachthree changecoachthree, String state, String agreed, String auditcomment) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(changecoachthree.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {
                changecoachthree.setCfbillstate(Long.valueOf("5"));

                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                changecoachthree.setFlastupdatetime(createTime);
                //修改人id
                changecoachthree.setFauditorid(user.getFid());
                changecoachthreeMapper.updateByPrimaryKeySelective(changecoachthree);
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                changecoachthree.setCfbillstate(Long.valueOf("3"));
                changecoachthreeMapper.updateByPrimaryKeySelective(changecoachthree);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            changecoachthree.setCfbillstate(Long.valueOf("3"));
            changecoachthree.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(changecoachthree.getFnumber(), auditcomment);
            changecoachthreeMapper.updateByPrimaryKeySelective(changecoachthree);
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    @Override
    public List<Changecoachthreeentry> selectChangecoachThreeregAsXls(String fnumber, String fdescription, String cfstudentname, String beginTime, String endTime, String fcompanyid) {
        return changecoachthreeMapper.selectChangecoachthreeregAsXls(fnumber, fdescription, cfstudentname, beginTime, endTime, fcompanyid);
    }

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.ChangecoachthreeMapper.pagecount", paramMap, null);
        return paramMap;
    }
}
